/**
 * 
 */
package com.wolfpire.subject.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.UserGroupSubjectRelationDao;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.RoleAuthorityRelation;
import com.wolfpire.system.model.Section;
import com.wolfpire.system.model.UserGroupSubjectRelation;

/**
 * 用户组课程关联关系
 * @author lihd
 *
 */

@Repository("userGroupSubjectRelationDao")
public class UserGroupSubjectRelationDaoImpl extends BaseHibernateDao<UserGroupSubjectRelation, Long> implements UserGroupSubjectRelationDao {

	public void relate(Long usergroupId, Long subjectId) {
		// TODO Auto-generated method stub
		this.deleteByUserGroupId(usergroupId);
		if (null != subjectId) {
			UserGroupSubjectRelation ps = new UserGroupSubjectRelation(usergroupId,subjectId);
			this.saveOrUpdate(ps);
		}
		
	}

	private void deleteByUserGroupId(Long usergroupId) {
		String executeSql = "DELETE FROM t_usergroup_subject WHERE usergroup_id = ?";
		this.executeSql(executeSql, usergroupId);
	}
	/**
	 * 获取用户组关联的学科
	 */
	public List<UserGroupSubjectRelation>  getUGSRelation(Long usergroupId){
		StringBuffer querySql = new StringBuffer();
		List<String> propertyNames = new ArrayList<String>();
		List propertyValues = new ArrayList();
		querySql.append(" select ugs.* from t_usergroup_subject ugs where ugs.usergroup_id = :usergroupId");
		propertyNames.add("usergroupId");
		propertyValues.add(usergroupId);
		SQLQuery query = this.createSqlQuery(querySql.toString(), propertyNames, propertyValues);
		query.addEntity(UserGroupSubjectRelation.class);
		return query.list();
	}
	
	/*public UserGroupSubjectRelation getUGSRelation(Long usergroupId) {
		Criteria c = createCriteria(Restrictions.eq("usergroupId", usergroupId));
		Object obj = c.uniqueResult();
		if (null != obj) {
			return (UserGroupSubjectRelation) obj;
		}
		return null;
	}*/

	public void relate(Long usergroupId, List<Long> subjectIds) {
		//先删除后绑定
		this.deleteByUserGroupId(usergroupId);
		if (CollectionUtils.isNotEmpty(subjectIds)) {
			Session session = this.getSession();
			int size = subjectIds.size();
			for (int i = 0; i < size; i++) {
				UserGroupSubjectRelation ps = new UserGroupSubjectRelation(usergroupId,subjectIds.get(i));
				//RoleAuthorityRelation ur = new RoleAuthorityRelation(roleId, authorityIds.get(i));
				session.save(ps);
				if ((i + 1) % 50 == 0) {
					session.flush();
					session.clear();
				}
			}
		}
		
	}


}
